﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace GenerateXmlFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog savefd = new SaveFileDialog();
            savefd.Filter = "XML Files (*.xml)|*.xml";
            savefd.RestoreDirectory = true;

            if (savefd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    XmlTextWriter xtw = new XmlTextWriter("Question.xml", null);
                    
                    xtw.Formatting = Formatting.Indented;
                    xtw.Indentation = 3;

                    //////////////////////////////////////////////////////////////////////////////////////
                    xtw.WriteStartDocument();
                    xtw.WriteStartElement("Datas");
                    for (int i = 0; i < m_dataGridView.Rows.Count - 1; i++)
                    {
                        m_dataGridView.Rows[i].Cells[0].Value = i + 1;
                        
                        xtw.WriteStartElement("Data");
                        
                        // Q
                        if (m_dataGridView.Rows[i].Cells[1].Value != null)
                        {
                            xtw.WriteElementString("Q", m_dataGridView.Rows[i].Cells[1].Value.ToString());
                        }
                        else
                        {
                            xtw.WriteElementString("Q", String.Empty);
                        }

                        // A
                        if (m_dataGridView.Rows[i].Cells[2].Value != null)
                        {
                            xtw.WriteElementString("A", m_dataGridView.Rows[i].Cells[2].Value.ToString());
                        }
                        else
                        {
                            xtw.WriteElementString("A", String.Empty);
                        }

                        // B
                        if (m_dataGridView.Rows[i].Cells[3].Value != null)
                        {
                            xtw.WriteElementString("B", m_dataGridView.Rows[i].Cells[3].Value.ToString());
                        }
                        else
                        {
                            xtw.WriteElementString("B", String.Empty);
                        }
                        
                        // R
                        if (m_dataGridView.Rows[i].Cells[4].Value != null)
                        {
                            xtw.WriteElementString("R", m_dataGridView.Rows[i].Cells[4].Value.ToString());
                        }
                        else
                        {
                            xtw.WriteElementString("R", String.Empty);
                        }
                       
                        xtw.WriteEndElement();
                    }
                    xtw.WriteEndElement();
                    xtw.WriteEndDocument();
                    xtw.Close();
                    //////////////////////////////////////////////////////////////////////////////////////

                    MessageBox.Show("Xuất file XML thành công", "Thông báo", MessageBoxButtons.OK);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xuất file XML không thành công", "Thông báo", MessageBoxButtons.OK);
                }
            }
        }

        private void m_dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }

}